package main

import (
	"fmt"

	"github.com/molio/practice/ds/bracketmatch"
	"github.com/molio/practice/ds/link"
	"github.com/molio/practice/ds/lottery"
	"github.com/molio/practice/ds/paging"
	"github.com/molio/practice/ds/palindrome"
	"github.com/molio/practice/ds/stack"
	"github.com/molio/practice/ds/utils"
)

func SingleLinkedListRun() {
	sll := link.NewSingleLinkedList()
	sll.CreateSingleLinkedList(true, 4)
	sll.TravelSingleLinkedList()

	// 尾插法
	// sll.InsertElementInTail()
	// sll.TravelSingleLinkedList()

	// 头插法
	// sll.InsertElementInHead()
	// sll.TravelSingleLinkedList()

	// 查找元素
	// fmt.Println(sll.FindElemnent())

	// 删除元素
	// node, _ := sll.FindElemnentByPos(3)
	// num, _ := node.Data.(int)
	// sll.DeleteElement(num)
	// sll.TravelSingleLinkedList()

	// 反转链表
	newSll := link.RevertSingleLinkedList(sll)
	newSll.TravelSingleLinkedList()
}

func CircularSingleLinkedListRun() {
	csll := link.NewCircularSingleLinkedList()
	csll.CreateCircularSingleLinkedList(true, 4)
	csll.TravelCircularSingleLinkedList()

	// // 尾插法
	// csll.InsertElementInTail(0, 100)
	// csll.TravelCircularSingleLinkedList()

	// // 头插法
	// csll.InsertElementInHead(0, 99)
	// csll.TravelCircularSingleLinkedList()

	// 查询
	// fmt.Println(csll.GetLength())
	// node := csll.FindElemnentByPos(2)
	// preNode := csll.FindElemnentPreNode(node.Data)
	// fmt.Println("找到前置结点： ", preNode.Data)

	// 删除元素
	node := csll.FindElemnentByPos(2)
	csll.DeleteElement(node.Data)
	csll.TravelCircularSingleLinkedList()
}

func DoubleLinkedListRun() {
	dll := link.NewDoubleLinkedList()
	dll.CreateDoubleLinkedList(4)
	dll.TravelDoubleLinkedList()

	// 头插法
	// dll.InsertElementInHead(0)
	// dll.TravelDoubleLinkedList()

	// time.Sleep(time.Second)
	// 尾插法
	// dll.InsertElementInTail(0)
	// dll.TravelDoubleLinkedList()

	// 删除元素
	node := dll.FindElemnentByPos(2)
	dll.DeleteElement(node.Data)
	dll.TravelDoubleLinkedList()
}

func CircularDoubleLinkedListRun() {
	cdll := link.NewCircularDoubleLinkedList()
	cdll.CreateCircularDoubleLinkedList(4)
	cdll.TravelCircularDoubleLinkedList()

	// 头插法
	// time.Sleep(time.Second)
	// cdll.InsertElementInHead()
	// cdll.TravelCircularDoubleLinkedList()

	// 尾插法
	// time.Sleep(time.Second)
	// cdll.InsertElementInTail()
	// cdll.TravelCircularDoubleLinkedList()

	// 删除元素
	node := cdll.FindElemnentByPos(2)
	cdll.DeleteElement(node.Data)
	cdll.TravelCircularDoubleLinkedList()
}

func LinkRun() {
	SingleLinkedListRun()
	CircularSingleLinkedListRun()
	DoubleLinkedListRun()
	CircularDoubleLinkedListRun()
	lottery.LotteryRun()
	paging.PageRun()
}

func StackRun() {
	// sequenceStack := stack.NewSequenceStack(10)
	// sequenceStack.TravelSequenceStack()

	// fmt.Printf("请输入压入栈中的元素: ")
	// sequenceStack.PushStack(utils.ReadInputString())
	// sequenceStack.TravelSequenceStack()

	// topElement := sequenceStack.PopStack()
	// fmt.Printf("出栈元素为: %v\n", topElement)

	// topElement := sequenceStack.GetTopStack()
	// fmt.Printf("栈顶元素为: %v\n", topElement)

	for {
		fmt.Println("请输入要检测的回文单词")
		word := utils.ReadInputString()
		if word == "" {
			break
		}
		fmt.Printf("%s 是否为回文单词: %v\n", word, palindrome.IsPalindromeWord(word))
	}
}

func LinkStackRun() {
	linkStack := stack.NewLinkStack()
	linkStack.PushStack(100)
	linkStack.PushStack(101)
	linkStack.PushStack(102)
	linkStack.TravelLinkStack()

	popNode := linkStack.PopStack()
	fmt.Printf("出栈元素：%v\n", popNode.Data)
	linkStack.TravelLinkStack()

	// 判断括号是否匹配
	fmt.Println(bracketmatch.IsBracketMatch("((34+23)*(23-14))/3"))
}

func main() {
	// LinkRun()
	// StackRun()
	LinkStackRun()
}
